package com.frt.sygang.controller;/**
 * Created by 冯瑞涛 on 2022/9/18 17:00
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.frt.sygang.common.R;
import com.frt.sygang.entity.Category;
import com.frt.sygang.sevice.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 冯瑞涛
 * @version 1.0
 * @date 2022/9/18 17:00
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    /**
     * 新增菜品类型和套餐类型方法
     * @param category
     * @return
     */
    @PostMapping
    public R<String> insCategory(@RequestBody Category category) {
        boolean save = categoryService.save(category);
        if(save){
            return R.success(null);
        }
        return R.error("添加失败");
    }

    @GetMapping("/page")
    public R<IPage> pageQueryCategory(Integer page,Integer pageSize){
        IPage<Category> pages=new Page<>();
        categoryService.page(pages);
        return R.success(pages);
    }

    @DeleteMapping
    public R<String> deleteCategory(Long ids){
        categoryService.remove(ids);
        return R.success("删除成功");
    }

    @PutMapping
    public R<String> updateCategory(@RequestBody Category category){
        boolean b = categoryService.updateById(category);
        if(b){
           return R.success(null);
        }
        return R.error("修改失败");
    }

    @GetMapping("/list")
    public R<List<Category>> listCategory(Category category){
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(queryWrapper);
        if(list!=null&&!list.isEmpty()){
            return R.success(list);
        }
        return R.error("未找到类型信息");
    }
}
